<?php
/*
 * Parser Class
 * 
 * Parses Template Files 
 * 
 * @author : Jason Meulenhoff
 * @mail : mail@kacangboi.com
 * @web : http://kacangboi.com
 * @copyright : KacangNetworks 2010
 * 
 */
class parser {

  public $code;
  public $_replacedata = array("block" => "","include" => "","var" => "");

  
  public function  __construct()
  {
  	debug::message("Template parser instance created","INFO");
  }
  public function loadTemplate($tFile) {
    if(file_exists($tFile)) {
    	debug::message("Opening the template $tFile ","INFO");
      	$this->code = serialize(file_get_contents($tFile));
      	debug::message("Template is loaded into the parser","INFO");    
    }else
     throw new Exception(debug::message("The template file $template does not exist","INFO"));
  }

  public function assignBlock($blockname,$array_met_values) {
    $block_content = $this->getContentInBlock($blockname);
    $this->_replacedata['block'][$blockname] = $array_met_values;
    $this->_replacedata['block'][$blockname]['contentblock'] = $block_content;
  }
     
  public function assignVar($var,$value) {
  	debug::message("replacing \{$var\} with $value","INFO");
    $this->_replacedata['var'][$var] = $value;
  }
    
  public function assignVars($vars,$replacements) {
  	debug::message("replacing multiple blocks with multiple values","INFO");
    if(!is_array($vars) || !is_array($replacements)) {
      $this->assignVar($vars,$replacements);
    } else {
      for($i=0; $i < count($vars); $i++) {
        $this->_replacedata['var'][$vars[$i]] = $replacements[$i];
      }
    }
  }

  public function assignIncludeContent($filename) {
    if(file_exists($filename)) {
      $this->_replacedata['include'][] = $filename;
    }
  }
  
  public function parse($display = FALSE) {
  	debug::message("Parsing the loaded template file","INFO");
    $code = unserialize($this->code);
    //de blocks verwerken
    if(is_array($this->_replacedata['block'])) {
      foreach($this->_replacedata['block'] as $blockname => $values) {
        $contentblock = $values['contentblock'];
        $block_output = '';
        for($i = 0; $i < (count($values) - 1);$i++) {
          $rij = $contentblock;
          foreach($values[$i] as $var => $replacement) {
            $rij = str_replace('{'.$var.'}',$replacement,$rij);
          }
          $block_output .= $rij;
        }
        $code = $this->replaceBlock($blockname,$block_output,$code);
      }
    }
    //de vars verwerken
    if(is_array($this->_replacedata['var'])) {
      foreach($this->_replacedata['var'] as $key => $value) {
        $code = str_replace('{'.$key.'}',$value,$code);
      }
    }
    if(is_array($this->_replacedata['include'])) {
      foreach($this->_replacedata['include'] as $file) {
        $contents_file = file_get_contents($file);
        $regex = "(\[file: ".$file."\])";
        $code = ereg_replace($regex,$contents_file,$code);
      }
    }
    if($display) {
      echo $code;
    } else {
      return $code;
    }
  }
  public function getContentInBlock($blockname) {
    $regex = "(.*)(\[begin-block: ".$blockname."\])(.*)(\[end-block: ".$blockname."\])(.*)";
    return ereg_replace($regex,"\\3",unserialize($this->code));
  }
        
  public function replaceBlock($blockname,$output,$code) {
    $regex = "(\[begin-block: ".$blockname."\])(.*)(\[end-block: ".$blockname."\])";
    return ereg_replace($regex,$output,$code);
  }
  
  public function clearSettings() {
    $this->__destruct();
  }
   
  public function __destruct() {
    $this->_replacedata = '';
    $this->code = '';
  }

}
?>
